package com.f.manage.utils;

import org.springframework.util.DigestUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 时间工具类
 */
public class DateUtil {
    public static final String TIME_PATTERN1 = "yyyy-MM-dd";
    public static final String TIME_PATTERN2 = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_PATTERN3 = "yyyyMMddHHmmss";
    public static final String TIME_PATTERN6 = "HH:mm";
    /***********将date格式化*****************************************/
    public static String formatDateTime(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN2);
        String sdtime = sdf.format(date);
        return sdtime;
    }
    public static String formatDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN1);
        String sdate = sdf.format(date);
        return sdate;
    }
    public static String formatHourMinus(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(TIME_PATTERN6);
        String hminus = sdf.format(date);
        return hminus;
    }
    /***********将LocalDateTime格式化*****************************************/
    public static String formatDateTime(LocalDateTime time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN2);
        String sdtime = dtf.format(time);
        return sdtime;
    }
    public static String formatDate(LocalDateTime time){
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(TIME_PATTERN1);
        String sdate = dtf.format(time);
        return sdate;
    }
    /***********将String转换date****************************************/
    public static Date parseToDate(String time) {
        if (time == null || time.equals("")) {
            return new Date();
        }
        try {
            String PATTERN = "";
            if(time.contains("T")){
                time = time.replace("T"," ");
            }
            if(time.length() > 10){//2012-12-12 12:12:12
                PATTERN = TIME_PATTERN2;
            }else{
                PATTERN = TIME_PATTERN1;
            }
            SimpleDateFormat sf = new SimpleDateFormat(PATTERN);
            Date date = sf.parse(time);
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    /***********将String转换LocalDate****************************************/
    public static LocalDate parseToLocalDate(String time) {
        if (time == null) {
            return LocalDate.now();
        }
        try {
            DateTimeFormatter fmt = DateTimeFormatter.ofPattern(TIME_PATTERN1);
            LocalDate date = LocalDate.parse(time, fmt);
            System.out.println("date:"+date);
            return date;
        } catch (Exception e) {
            return LocalDate.now();
        }
    }
    public static LocalDateTime parseToLocalDateTime(String time) {
        if (time == null) {
            return LocalDateTime.now();
        }
        try {
            if (time.contains("+")) {
                ZonedDateTime zonedDateTime = ZonedDateTime.parse(time);
                return zonedDateTime.toLocalDateTime();
            } else {
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern(TIME_PATTERN2);
                LocalDateTime date = LocalDateTime.parse(time, fmt);
                return date;
            }
        } catch (Exception e) {
            return LocalDateTime.now();
        }
    }
    /***********将Date-->LocalDateTime格式化*****************************************/
    public static LocalDateTime dateToLocalDateTime(Date time){
        try {
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt;
        } catch (Exception e) {
            e.printStackTrace();
            return LocalDateTime.now();
        }
    }
    public static LocalDate dateToLocalDate(Date time){
        try {
            LocalDateTime ldt = time.toInstant().atZone( ZoneId.systemDefault()).toLocalDateTime();
            return ldt.toLocalDate();
        } catch (Exception e) {
            e.printStackTrace();
            return LocalDate.now();
        }
    }
    /***********将LocalDateTime-->Date格式化*****************************************/
    public static Date localDateTimeToDate(LocalDateTime time){
        try {
            Date date = Date.from(time.atZone(ZoneId.systemDefault()).toInstant());
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    public static Date localDateToDate(LocalDate time){
        try {
            Date date = Date.from(time.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
            return date;
        } catch (Exception e) {
            return new Date();
        }
    }
    public static LocalDateTime getHourStart(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime tempTime = LocalDateTime.of(now.getYear(), now.getMonthValue(), now.getDayOfMonth(), now.getHour(),0,0);
        return tempTime;
    }
    public static String getDate5Minus(){
        LocalDateTime now = LocalDateTime.now();
        int minus = now.getMinute();
        if(minus<5){
            minus = 0;
        }else if(minus<10 && minus>=5){
            minus = 5;
        }else if(minus<15 && minus>=10){
            minus = 10;
        }else if(minus<20 && minus>=15){
            minus = 15;
        }else if(minus<25 && minus>=20){
            minus = 20;
        }else if(minus<30 && minus>=25){
            minus = 25;
        }else if(minus<35 && minus>=30){
            minus = 30;
        }else if(minus<40 && minus>=35){
            minus = 35;
        }else if(minus<45 && minus>=40){
            minus = 40;
        }else if(minus<50 && minus>=45){
            minus = 45;
        }else if(minus<55 && minus>=50){
            minus = 50;
        }else if(minus>=55){
            minus = 55;
        }
        String result = LocalDate.now().toString()+" "+now.getHour()+":"+minus+":00";
        return result;
    }

    public static String dateToString() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat(TIME_PATTERN2);
        String timestamp = dateFormat.format(date);
        return timestamp;
    }
    private List<Map<String, Object>> initialDateList(int start) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH");
        LocalDateTime day = LocalDateTime.now();
        Map<String, Object> map = new HashMap();
        for (int i = start; i < start + 24; i++) {
            day = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusHours(i);
            String date = day.format(formatter);
            map = new HashMap();
            map.put("time", date);
            map.put("num", 0);
            list.add(map);
        }
        return list;
    }

    private List<Map<String, Object>> initialDateList2(int start) {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH");
        LocalDateTime day = LocalDateTime.now();
        Map<String, Object> map = new HashMap();
        for (int i = start; i < start + 24; i++) {
            day = LocalDateTime.of(LocalDate.now(), LocalTime.MIN).plusHours(i);
            String date = day.format(formatter);
            map = new HashMap();
            map.put("time", date);
            map.put("num", 0);
            list.add(map);
        }
        return list;
    }

    public List<LocalDateTime> initialDateListNormol() {
        List<LocalDateTime> list = new ArrayList<LocalDateTime>();
        LocalDateTime day = LocalDateTime.now();
        for (int i = 7; i > 0; i--) {
            day = LocalDateTime.of(LocalDate.now().minusDays(i), LocalTime.MAX);
            list.add(day);
        }
        return list;
    }

    public List<LocalDate> initialDateListNormo2() {
        List<LocalDate> list = new ArrayList<LocalDate>();
        LocalDate day = LocalDate.now();
        for (int i = 6; i >= 0; i--) {
            day = LocalDate.now().minusDays(i);
            list.add(day);
        }
        return list;
    }

    public List<Map<String, Object>> changeList(List<Map<String, Object>> list) {
        List<Map<String, Object>> res = initialDateList(0);
        res.forEach(e -> {
            String outDate = String.valueOf(e.get("time"));
            list.forEach(map -> {
                String inDate = String.valueOf(map.get("time"));
                if (outDate.equals(inDate)) {
                    e.put("num", map.get("num"));
                }
            });
        });
        return res;
    }

    public List<Map<String, Object>> changeList2(List<Map<String, Object>> list) {
        List<Map<String, Object>> res = initialDateList2(0);
        res.forEach(e -> {
            String outDate = String.valueOf(e.get("time"));
            list.forEach(map -> {
                String inDate = String.valueOf(map.get("time"));
                if (outDate.equals(inDate)) {
                    e.put("num", map.get("num"));
                }
            });
        });
        return res;
    }

    public List<Map<String, Object>> changeListNotNow(List<Map<String, Object>> list) {
        List<Map<String, Object>> res = initialDateList(1);
        res.forEach(e -> {
            String outDate = String.valueOf(e.get("time"));
            list.forEach(map -> {
                String inDate = String.valueOf(map.get("time"));
                if (outDate.equals(inDate)) {
                    e.put("num", map.get("num"));
                }
            });
        });
        return res;
    }

    public static String LocalDateTimeFormat(LocalDateTime time){
       String start = time.format(DateTimeFormatter.ofPattern(TIME_PATTERN2));
       return start;
    }
    public static String LocalDateTimeStartFormat(LocalDateTime time){
        String start = LocalDateTime.of(time.toLocalDate(), LocalTime.MIN).format(DateTimeFormatter.ofPattern(TIME_PATTERN2));
        return start;
    }
    public static void main(String[] args) {
     /*   Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE,-1);
        Date time = calendar.getTime();
        String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(time);
        System.out.println(yesterday);*/
        DateUtil dateUtil = new DateUtil();

        System.out.println(12);

    }
}
